package com.ankiang.mykeys;

import android.util.Log;

import com.ankiang.mykeys.common.Constant;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;

/**
 * Created by guosen on 2017/4/24.
 */

class Utils {
    public static void CopyStream(InputStream is, OutputStream os) {
        final int buffer_size = 1024;
        try {
            byte[] bytes = new byte[buffer_size];
            for (; ; ) {
                int count = is.read(bytes, 0, buffer_size);
                if (count == -1)
                    break;
                os.write(bytes, 0, count);
                is.close();
                os.close();
            }
        } catch (Exception ex) {
        }
    }

    /**
     * 判断是否为空字符串，null也算空字串
     *
     * @param s
     * @return
     */
    public static boolean isEmpty(String s) {
        if (s == null) {
            return true;
        }
        if ("".equals(s)) {
            return true;
        }
        return false;
    }

    /**
     * 取指定Key的值，如果没有则返回""
     *
     * @param json
     * @param key
     * @return
     */
    public static String getStringFromJson(JSONObject json, String key) {
        String s;
        if (json == null) {
            Log.e("Utils", "非法JsonObject");
            s = "";
        } else {
            try {
                s = json.getString(key);
                if (s == null) {
                    s = "";
                }
                return s;
            } catch (JSONException e) {
                e.printStackTrace();
                s = "";
                Log.e("Error", e.getMessage());
            }
        }
        Log.i("Utils", key + "=" + s);
        return s;
    }

    public static JSONArray getJsonArray(String key, String json) throws JSONException {

        JSONArray resultData = new JSONArray();
        JSONObject response = new JSONObject(json);
        if (key.equals(Constant.K_APP_ALL)) { // 所有的应用列表
            JSONObject res = (JSONObject) response.get("res");
            int code = res.getInt("code");
            if (code != 0) {
                String msg = res.getString("msg");
                return new JSONArray();
            }
            JSONArray mData = response.getJSONArray("data");
            for (int i = 0; i < mData.length(); i++) {
                JSONObject obj = (JSONObject) mData.get(i);
                int iconId = 0;
//                int iconId = Utils.getIntFromJson(obj, "icon");
                String title = Utils.getStringFromJson(obj, "name");
                String content = Utils.getStringFromJson(obj, "name");
                String comments = Utils.getStringFromJson(obj, "des");
                JSONObject tmp = new JSONObject();
                tmp.put("icon", iconId);
                tmp.put("title", title);
                tmp.put("content", content);
                tmp.put("comments", comments);
                resultData.put(tmp);
            }

        } else if (key.startsWith(Constant.K_APP_PREFIX)) { // APP下的快捷键列表
            JSONObject res = (JSONObject) response.get("res");
            int code = res.getInt("code");
            if (code != 0) {
                String msg = res.getString("msg");
                return new JSONArray();
            }
            JSONArray mData = response.getJSONArray("data");
            for (int i = 0; i < mData.length(); i++) {
                JSONObject obj = (JSONObject) mData.get(i);
                int iconId = 0;
//                int iconId = Utils.getIntFromJson(obj, "icon");
                String title = Utils.getStringFromJson(obj, "shortcut");
                String content = Utils.getStringFromJson(obj, "name");
                String comments = Utils.getStringFromJson(obj, "des");
                JSONObject tmp = new JSONObject();
                tmp.put("icon", iconId);
                tmp.put("title", title);
                tmp.put("content", content);
                tmp.put("comments", comments);
                resultData.put(tmp);
            }
        } else if (key.startsWith(Constant.K_USER_PREFIX)) { // 发现里的数据

            JSONObject res = response;
            int code = res == null ? 1 : 0;
            if (code != 0) {
                String msg = "没有取到数据";
                return new JSONArray();
            }
            //TODO kaven
            JSONArray mData = response.getJSONArray("item");
            return mData;
        }

        Log.i("Utils-->", resultData.toString());

        return resultData;
    }

    public static int getIntFromJson(JSONObject json, String key) {
        int result = 0;
        if (json == null) {
            Log.e("Utils", "非法JsonObject");
            result = 0;
        } else {
            try {
                String s = json.getString(key);
                if (s == null) {
                    Log.e("Utils", "非法Key=" + key);
                    result = 0;
                } else {
                    result = json.getInt(key);
                }
            } catch (JSONException e) {
                e.printStackTrace();
                Log.e("Error", e.getMessage());
                result = 0;
            }
        }
        Log.i("Utils", key + "=" + result);
        return result;
    }
}
